<?php

class Admin_Model_MOrder
{
    protected $_connection=null;
    protected $_db=null;
    protected $_collection=null;
    private $_function=null;

    public function  __construct()
    {
       $this->_connection=new Mongo();
       $this->_db=$this->_connection->bookstore;
       $this->_collection=$this->_db->order;
       $this->_function=new Default_Model_MFunction();
    }

    public function getNewOrder(&$total,$currentpage=1,$pagesize=10)
    {
        $total=$this->_collection->find(array("status"=>0),array("_id"=>true))->count();
        if($total==0)
            return array();
        if(!is_int($currentpage) || !is_int($pagesize))
            return array();

        if($currentpage<=0)
            $currentpage=1;
        $numberpage=$total/$pagesize;
        if($total%$pagesize!=0)
            $numberpage++;
        if($currentpage>$numberpage)
            $currentpage=$numberpage;
        
        return $this->_collection->find(array("status"=>0),array("_id"=>true,"code_order"=>true,"date_order"=>true))
                ->sort(array("date_order"=>-1))->skip(($currentpage-1)*$pagesize)->limit($pagesize);
    }
    public function getCompleteOrder(&$total,$currentpage=1,$pagesize=10)
    {
        $total=count($this->_collection->find(array("status"=>1),array("_id"=>true)));
        if($total==0)
            return array();
        if(!is_int($currentpage) || !is_int($pagesize))
            return array();

        if($currentpage<=0)
            $currentpage=1;
        $numberpage=$total/$pagesize;
        if($total%$pagesize!=0)
            $numberpage++;
        if($currentpage>$numberpage)
            $currentpage=$numberpage;

        return $this->_collection->find(array("status"=>1),array("_id"=>true,"code_order"=>true,"date_order"=>true))
               ->sort(array("date_order"=>-1))
               ->skip(($currentpage-1).$pagesize)->limit($pagesize) ;
    }

    public function getInfoOrder($oid)
    {
        $oOid=new MongoId($oid);
        $order=$this->_collection->findOne(array("_id"=>$oOid));
        if($order==null)
            return array();

        $orderInfo=array();
        $orderInfo["_id"]=$order["_id"];
        
        $arrDate=$this->_function->stringDateToArray($order["date_order"]);
        $orderInfo["date_order"]=$arrDate["day"].'/'.$arrDate["month"].'/'.$arrDate["year"];
        $orderInfo["code_order"]=$order["code_order"];
        $bookOrder=array();
        $bookModel=new Admin_Model_MBook();
        foreach($order["book"] as $abookOrder)
        {  
            $abook=$bookModel->getBookForPay($abookOrder["id_book"]);
            if(count($abook)>0)
            {
                $abook["number"]=$abookOrder["number"];
                array_push($bookOrder, $abook);
            }
        }
        $orderInfo["book"]=$bookOrder;

        // lay thong tin nguoi mua (nguoi duoc tang neu tang sach)
        $typebuy=0; // mua
        if($order["id_user"]!=$order["id_user_receive"])
            $typebuy=1;
        $orderInfo["typebuy"]=(int)$typebuy;

        $userModel=new Personal_Model_MUser();
        if((int)$order["type_user"]==1) // register
        {
            $userinfo=$userModel->getPersonalInfo($order["id_user"]);
            $orderInfo["user_info"]=$userinfo;
        }
        else if ((int)$order["type_user"]==-1) // public
        {
            $userinfo=$userModel->getPublicPersonalInfo($order["id_user"]);
            $orderInfo["user_info"]=$userinfo;
        }
        if((int)$orderInfo["user_info"]["sex"]==1)
            $orderInfo["user_info"]["sex"]="Nam";
        else
            $orderInfo["user_info"]["sex"]="Nữ";
        
        if($typebuy==1)
        {
            $userinfo=$userModel->getPublicPersonalInfo($order["id_user_receive"]);
            $orderInfo["user_info_receive"]=$userinfo;

            if((int)$orderInfo["user_info_receive"]["sex"]==1)
                $orderInfo["user_info_receive"]["sex"]="Nam";
            else
                $orderInfo["user_info_receive"]["sex"]="Nữ";
        }

        return $orderInfo;
    }
}
?>
